/*
 * @Description: 
 * @version: 
 * @Author: CHEN
 * @Date: 2020-12-27 16:54:34
 * @LastEditors: CHEN
 * @LastEditTime: 2020-12-27 16:58:30
 */

$(function () {
    let basePath = "/SCSystem/"
    $('form').bootstrapValidator({
        message: 'This value is not valid',
        excluded: ':disabled',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            userName: {
                validators: {
                    notEmpty: {
                        message: '用户名不为空'
                    },
                    regexp: {
                        regexp: /^[\d]+$/,
                        message: '用户名只能为数字'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: "密码不能为空"
                    }
                }
            }
        }
    });

    $("#submitBtn").click(function () {//非submit按钮点击后进行验证，如果是submit则无需此句直接验证
        $("form").bootstrapValidator('validate');//提交验证
        if ($("form").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
            $.ajax({
                url:"LoginServlet",
                type:"POST",
                datatype: 'json',
                data : {
                    userName :$('[name="userName"]').val(),
                    password :$('[name="password"]').val(),
                    role : $('[name="role"]:checked').val()
                },
                success : function(msg) {
                    if (!msg.flag){
                        alert("密码错误!如果忘记密码请联系教务处工作人员")
                    }else {
                        window.location.replace(basePath + "html/main.html")
                    }
                },
                error : function() {
                    alert("登录失败,请检查网络配置")
                }
            })
        }
    });
});